from kafka import KafkaProducer
import json

# 指定Kafka集群中的地址（可指定多个，用逗号分隔）线上
# bootstrap_servers = ['106.120.201.126:14576']
bootstrap_servers = ['10.102.1.113:9092']

# 指定主题
topicName = 'CA002MatchResultData'
topicName_webgl='CA002MatchResultData_webgl'

# 创建KafkaProducer对象
producer = KafkaProducer(bootstrap_servers = bootstrap_servers, value_serializer = lambda x: json.dumps(x).encode('utf-8'))

# 创建消息
# data = {'name': '张三', 'age': 25, 'address': '广州市天河区'}


def sendKafamsg(data,topicType):
    # 通过KafkaProducer对象发送消息，topic为指定的主题
    # print(data)
    # print(type(data))
    if topicType=="webgl":
        topicType=topicName_webgl
    else:
        topicType=topicName
    producer.send(topicType, value=data)
# 关闭KafkaProducer对象
# producer.close()
